package com.zsj.juc.completableFuture;

import java.util.concurrent.*;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/4/18 8:53
 * @description
 */
public class FutureTest01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        System.out.println(start);
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        FutureTask<String> futureTask = new FutureTask<>(new MyThread());
        threadPool.submit(futureTask);
        while (true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());
                break;
            } else {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                    System.out.println("正在处理中");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end);
        System.out.println("消耗时间" + (end - start) + "毫秒");
        threadPool.shutdown();
    }
}

class MyThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        TimeUnit.SECONDS.sleep(1);
        return "hello";
    }
}
